/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import org.openaecfm.model.geometry.BSplineCurve;
import org.openaecfm.model.geometry.BSplineCurveWithKnots;
import org.openaecfm.model.geometry.KnotType;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class BSplineCurveWithKnotsIO extends
		AbstractEntityInstanceInputImpl<BSplineCurveWithKnots> implements
		IEntityInstanceOutput<BSplineCurveWithKnots>,
		IEntityInstanceUtilOutput<BSplineCurveWithKnots>,
		IEntityInstanceUtilInput<BSplineCurveWithKnots> {

	private IEntityInstanceUtilOutput<BSplineCurve> utilOutput;
	private IEntityInstanceUtilInput<BSplineCurve> utilInput;

	public void setUtilOutput(IEntityInstanceUtilOutput<BSplineCurve> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<BSplineCurve> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void exportSPF(BSplineCurveWithKnots pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set KnotMultiplicities
		ListParameter knotMultiplicities = new ListParameter();
		for (Integer i : pojo.getKnotMultiplicities()) {
			knotMultiplicities.add(new IntegerParameter(i));
		}
		sei.addParameter(knotMultiplicities);

		// Set Knots
		ListParameter knots = new ListParameter();
		for (Double d : pojo.getKnots()) {
			knots.add(new RealParameter(d));
		}
		sei.addParameter(knots);

		// Set KnotSpec
		sei.addParameter(new EnumerationParameter(pojo.getKnotSpec()));
	}

	@Override
	public String exportSPF(BSplineCurveWithKnots pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCBSPLINECURVEWITHKNOTS", appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			BSplineCurveWithKnots pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	@Override
	protected BSplineCurveWithKnots doCreateObject() {
		return new BSplineCurveWithKnots();
	}

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			BSplineCurveWithKnots pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set KnotMultiplicities
		ListParameter knotMultiplicities = (ListParameter) sei.getParameter(5);
		for (Parameter p : knotMultiplicities) {
			IntegerParameter i = (IntegerParameter) p;
			pojo.getKnotMultiplicities().add(i.toInteger());
		}

		// Set Knots
		ListParameter knots = (ListParameter) sei.getParameter(6);
		for (Parameter p : knots) {
			RealParameter r = (RealParameter) p;
			pojo.getKnots().add(r.toDouble());
		}

		// Set KnotSpec
		EnumerationParameter knotSpec = (EnumerationParameter) sei
				.getParameter(7);
		pojo.setKnotSpec(KnotType.valueOf(knotSpec.toEnumString()));
	}
}
